package com.ibeeking.found.msg.rest.service.impl;

import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.msg.rest.service.IWxMpAutoMessageKeyService;
import com.ibeeking.found.msg.service.common.dos.WxMpAutoMessageDO;
import com.ibeeking.found.msg.service.common.dos.WxMpAutoMessageKeyDO;
import com.ibeeking.found.msg.service.common.param.WxMpAutoMessageKeyParam;
import com.ibeeking.found.msg.service.common.query.WxMpAutoMessageKeyPageQuery;
import com.ibeeking.found.msg.service.common.query.WxMpAutoMessageKeyQuery;
import com.ibeeking.found.msg.service.common.vo.WxMpAutoMessageKeyVO;
import com.ibeeking.found.msg.service.mapper.WxMpAutoMessageKeyMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName WxMpAutoMessageKeyServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-02-05 19:10
 **/
@Service
public class WxMpAutoMessageKeyServiceImpl extends ServiceImpl<WxMpAutoMessageKeyMapper, WxMpAutoMessageKeyDO> implements IWxMpAutoMessageKeyService {

    @Resource
    private WxMpAutoMessageKeyMapper wxmpAutoMessageKeyMapper;

    @Override
    public List<WxMpAutoMessageKeyVO> list(WxMpAutoMessageKeyQuery query){
        List<WxMpAutoMessageKeyDO> wxMpAutoMessageKeyDOS = wxmpAutoMessageKeyMapper.selectList(new LambdaQueryWrapper<WxMpAutoMessageKeyDO>());
        return JsonUtils.jsonToList(wxMpAutoMessageKeyDOS, WxMpAutoMessageKeyVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<WxMpAutoMessageKeyVO> pageList(WxMpAutoMessageKeyPageQuery pageQuery){

        Page<WxMpAutoMessageKeyDO> pages = wxmpAutoMessageKeyMapper.selectPage(new Page<WxMpAutoMessageKeyDO>(), new LambdaQueryWrapper<WxMpAutoMessageKeyDO>());
        return BeanUtil.convertPage(pages , WxMpAutoMessageKeyVO.class);
    }

    @Override
    public Boolean add(WxMpAutoMessageKeyParam param){
        WxMpAutoMessageKeyDO wxmpAutoMessageKeyDO = BeanUtil.convertBean(param, WxMpAutoMessageKeyDO.class);
        wxmpAutoMessageKeyDO.setCreateTime(LocalDateTime.now());
        wxmpAutoMessageKeyDO.setModifyTime(LocalDateTime.now());
        return wxmpAutoMessageKeyMapper.insert(wxmpAutoMessageKeyDO)>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(WxMpAutoMessageDO messageDO, List<String> keys){

        if(this.check(keys,messageDO.getId(),messageDO.getAppId())){
            throw new BusinessException("关键字不能重复");
        }
        if(Collections3.isNotEmpty(keys)){
            List<WxMpAutoMessageKeyDO> dos = keys.stream().map(key->{
                WxMpAutoMessageKeyDO wxmpAutoMessageKeyDO = new WxMpAutoMessageKeyDO();
                wxmpAutoMessageKeyDO.setAllEquals(1);
                wxmpAutoMessageKeyDO.setAutoMessageId(messageDO.getId());
                wxmpAutoMessageKeyDO.setAppId(messageDO.getAppId());
                wxmpAutoMessageKeyDO.setCreateBy(messageDO.getCreateBy());
                wxmpAutoMessageKeyDO.setModifyBy(messageDO.getModifyBy());
                wxmpAutoMessageKeyDO.setCreateTime(LocalDateTime.now());
                wxmpAutoMessageKeyDO.setModifyTime(LocalDateTime.now());
                wxmpAutoMessageKeyDO.setKeyWord(key);
                return wxmpAutoMessageKeyDO;
            }).collect(Collectors.toList());
            return this.wxmpAutoMessageKeyMapper.insertBatchSomeColumn(dos) == keys.size();
        }else {
            return true;
        }

    }

    @Override
    public Boolean modify(WxMpAutoMessageKeyParam param){
        WxMpAutoMessageKeyDO wxmpAutoMessageKeyDO = BeanUtil.convertBean(param, WxMpAutoMessageKeyDO.class);
        wxmpAutoMessageKeyDO.setModifyTime(LocalDateTime.now());
        return wxmpAutoMessageKeyMapper.updateById(wxmpAutoMessageKeyDO)>0;
    }

    @Override
    public WxMpAutoMessageKeyVO view(Long id){
        WxMpAutoMessageKeyDO wxmpAutoMessageKeyDO = wxmpAutoMessageKeyMapper.selectById(id);
        return BeanUtil.convertBean(wxmpAutoMessageKeyDO, WxMpAutoMessageKeyVO.class);
    }



    @Override
    public Boolean del(Long id){
        return wxmpAutoMessageKeyMapper.deleteById(id)>0;
    }

    /**
     * 通过message批量删除
     *
     * @param messageId
     * @return
     */
    @Override
    public Boolean delByMessageId(Long messageId) {
        QueryWrapper<WxMpAutoMessageKeyDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WxMpAutoMessageKeyDO::getAutoMessageId,messageId);
        return this.wxmpAutoMessageKeyMapper.delete(queryWrapper)>0;
    }

    @Override
    public List<WxMpAutoMessageKeyVO> queryListByMessageId(Long id) {
        QueryWrapper<WxMpAutoMessageKeyDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WxMpAutoMessageKeyDO::getAutoMessageId,id);
        return JsonUtils.jsonToList(this.wxmpAutoMessageKeyMapper.selectList(queryWrapper), WxMpAutoMessageKeyVO.class);
    }

    private boolean check(List<String> list , Long id , String appId){

        if(Collections3.isEmpty(list)){
            return false;
        }

        QueryWrapper<WxMpAutoMessageKeyDO> queryWrapper = new QueryWrapper<>();
        for (String s : list){
            if(list.contains(s)){
                return false;
            }
            queryWrapper.lambda().eq(WxMpAutoMessageKeyDO::getAppId,appId)
                    .eq(WxMpAutoMessageKeyDO::getKeyWord,s)
                    .ne(id!=null, WxMpAutoMessageKeyDO::getId,id);
            if(Collections3.isNotEmpty(this.wxmpAutoMessageKeyMapper.selectList(queryWrapper))){
                return false;
            }
            queryWrapper.clear();
        }

        return true;

    }
}
